<?php
    error_reporting(E_ALL || ~E_NOTICE);
	$erDeveDc=@$_POST["de"];
//    $string = "erDeve:".$erDeve."\n";
//    @file_put_contents("test.log", $string, FILE_APPEND);
//    $erDeveDc = base64_decode($erDeve);
//    $string = "erDeve:".$erDeveDc."\n";
//    @file_put_contents("test.log", $string, FILE_APPEND);
//连接到本地mysql数据库,登录数据库用户名、密码
    $myconn=@mysql_connect("localhost","root","root");
    if(!$myconn)
    {
        die('Could not connect: '.mysql_error());
		$string = "Could not connect: ".mysql_error();
        file_put_contents("test.log", $string, FILE_APPEND);
		echo "{\"res\":\"error\"}";
		return;
    }	
//选择dwaccessDB为操作库    
	$db_selected=mysql_select_db("dwaccessDB",$myconn);
	if(!$db_selected)
    {        
    	die("Can't use database dwaccessDB : ".mysql_error());
	    $string = "Can't use database : ".mysql_error();
        file_put_contents("test.log", $string, FILE_APPEND);
		echo "{\"res\":\"error\"}";
		return;
	}
	if(!strstr($erDeveDc,"\"controlerNum\":\"") && !strstr($erDeveDc,"\"cmd_code\":\"KQ_INIT\""))
	{
	    $response = base64_encode("{\"res\":\"deny5\"}");
		echo $response;
		@file_put_contents("test.log", "response =".$response, FILE_APPEND);
	}else 
	{
	    $index1 = substr($erDeveDc,17);
//		file_put_contents("test.log", "index1 =".$index1, FILE_APPEND);
		$index2 = strpos($index1,"\"");
//		file_put_contents("test.log", "index2 =".$index2, FILE_APPEND);
		if($index2==0)
        {
		    $response = base64_encode("{\"res\":\"deny3\"}");
		    echo $response;
//		    file_put_contents("test.log", "response =".$response, FILE_APPEND); 
		}else if($index2>24)
        {
		    $response = base64_encode("{\"res\":\"deny6\"}");
		    echo $response;
//		    file_put_contents("test.log", "response =".$response, FILE_APPEND); 
		}else
        {
//		    $ctrNumLen = $index2;
			$controlerNum = substr($index1,0,$index2);
//			file_put_contents("test.log", "controlerNum = ".$controlerNum." ctrNumLen= ".$index2, FILE_APPEND);
			if(!strstr($erDeveDc,"\"cmd_mode\":\"KQ_INIT\""))
	        {
	            $response = base64_encode("\"res\":\"deny5\"}");
		        echo $response;
//		        file_put_contents("test.log", "response0 =".$response, FILE_APPEND);
	        }else
	        {
		        $strSql = "select distinct controlerNum from gatesetting where controlerNum=\"".$controlerNum."\";";			
                //用mysql_query函数从gatesetting表里读取数据
                $result=mysql_query($strSql,$myconn);
                //通过循环读取数据内容	
                $deRE = array();
                while($row=mysql_fetch_array($result))
                {
                    $deRE = $row;
//			        file_put_contents("test.log", "  row[0] =".$row[0]."  row[1] =".$row[1], FILE_APPEND); 
                }
                //释放资源
	            mysql_free_result($result);
                if(!$deRE[0])
		        {
		            $response = base64_encode("{\"res\":\"deny1\"}");
		            echo $response;
//		            file_put_contents("test.log", "  response160: ".$response, FILE_APPEND); 
		        }else 
		        {
                    $transid=uuid();
					$strSql="INSERT INTO cmdform(controlerNum,transid,cmdstr) VALUES('".$controlerNum."','".$transid."','{\"trans_id\":\"".$transid."\",\"cmd_code\":\"KQ_INIT\"}');";
                    $result=mysql_query($strSql,$myconn);
					mysql_free_result($result);
					$response = base64_encode("{\"res\":\"success\"}");
		            echo $response;
//                    file_put_contents("test.log", "\n strSql =".$strSql, FILE_APPEND); 					
				}				
			}
	    }	    
	}
//关闭对数据库的连接
    mysql_close($myconn);
//	function _object2array($object) { return @json_decode(@json_encode($object),1); }	
	function uuid() 
	{
        if (function_exists('com_create_guid')) 
		{
            return md5(com_create_guid());
        }else 
		{
            mt_srand((double)microtime()*10000);                   //optional for php 4.2.0 and up.
            $charid = md5(uniqid(rand(),true)); 
//            $charid = strtoupper ( md5 ( uniqid ( rand (), true ) ) ); 
//            $hyphen = chr ( 45 );                                          // "-"
//            $uuid = '' .                                                   //chr(123)// "{"
//            substr ( $charid, 0, 8 ) . $hyphen . substr ( $charid, 8, 4 ) . $hyphen . substr ( $charid, 12, 4 ) . $hyphen . substr ( $charid, 16, 4 ) . $hyphen . substr ( $charid, 20, 12 );                                                                           //.chr(125);// "}"
            return $charid;
        }
    }
?>
